package com.vng.zing.survey.server;

import com.vng.zing.lib.Config;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import com.vng.zing.survey.controller.AjaxUpdateSurvey;
import com.vng.zing.survey.controller.AjaxAddSurvey;
import com.vng.zing.survey.controller.AjaxDeleteResponse;
import com.vng.zing.survey.controller.AjaxDeleteSurvey;
import com.vng.zing.survey.controller.AjaxGetAnswerByQuestion;
import com.vng.zing.survey.controller.AjaxGetReponseDetail;
import com.vng.zing.survey.controller.AjaxHome;
import com.vng.zing.survey.controller.AjaxResponseDetail;
import com.vng.zing.survey.controller.AjaxStatisticController;
import com.vng.zing.survey.controller.AjaxSubmitResponse;
import com.vng.zing.survey.controller.EditSurveyController;
import com.vng.zing.survey.controller.HomeController;
import com.vng.zing.survey.controller.NewSurveyController;
import com.vng.zing.survey.controller.ResponseController;
import com.vng.zing.survey.controller.ResponseDetailController;
import com.vng.zing.survey.controller.RoutingController;
import com.vng.zing.survey.controller.StatisticController;
import com.vng.zing.survey.controller.TestController;
import com.vng.zing.survey.controller.test.AddSurveyControllerTest;
import com.vng.zing.survey.filter.RoutingFilter;

public class WebServer {

    public void start() throws Exception {
        //Server server = new Server();
        //init notifybus
        //CommonModel.init();

        ThreadPool theadPool = new QueuedThreadPool(100, 3000);

        Server server = new Server(theadPool);


        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(8443);
        http_config.setOutputBufferSize(32768);
        ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
        http.setPort(Config.getParamInt("jetty_server", "port"));
        http.setIdleTimeout(30000);
        server.setConnectors(new Connector[]{http});


        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.addFilter(RoutingFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        handler.addServlet(AddSurveyControllerTest.class, "/add/test");
        handler.addServlet(RoutingController.class, "/*");
        handler.addServlet(AjaxAddSurvey.class, "/add");
        handler.addServlet(AjaxSubmitResponse.class, "/submit");
        handler.addServlet(AddSurveyControllerTest.class, "/add/test");
        handler.addServlet(AjaxUpdateSurvey.class, "/edited");
        handler.addServlet(AjaxStatisticController.class, "/answer");
        handler.addServlet(AjaxResponseDetail.class, "/responsedetail");
        handler.addServlet(AjaxHome.class, "/paging");
        handler.addServlet(AjaxDeleteResponse.class, "/delres");
        handler.addServlet(AjaxDeleteSurvey.class, "/delsur");
        handler.addServlet(HomeController.class, "/home/*");
        handler.addServlet(NewSurveyController.class, "/new/*");
        handler.addServlet(ResponseController.class, "/response/*");
        handler.addServlet(StatisticController.class, "/stat/*");
        handler.addServlet(EditSurveyController.class, "/edit/*");
        handler.addServlet(ResponseDetailController.class, "/detail/*");
        handler.addServlet(AjaxGetAnswerByQuestion.class, "/getanswer/*");
        handler.addServlet(AjaxGetReponseDetail.class, "/getresponse/*");
        handler.addServlet(TestController.class, "/test/*");
        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setResourceBase(Config.getParamStr("resource", "resource"));

        ContextHandler context0 = new ContextHandler();
        context0.setContextPath("/");
        context0.setHandler(resource_handler);

        HandlerList handlers = new HandlerList();

        handlers.setHandlers(new Handler[]{context0, handler});
        server.setHandler(handlers);
        server.setStopAtShutdown(true);
        server.start();
        server.join();
    }
}
